package com.lkz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkz.entity.Customer;
import com.lkz.entity.Employee;
import com.lkz.entity.Role;
import com.lkz.service.ICustomerService;
import com.lkz.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *  前端控制器
 *
 */
@RestController
public class CustomerController {
    @Autowired
    private ICustomerService customerService;


    @PostMapping(value = "/customer/uploadExcl")
    @ResponseBody
    public  Map<String ,Object> uploadExcl(HttpServletRequest request, @RequestParam("file") MultipartFile file){
        Map<String ,Object> result = new HashMap<>();
        String path = request.getSession().getServletContext().getRealPath("/");
        try{
            if(!file.isEmpty()){
                result = customerService.uploadExcl(file);
            }else {
                result.put("code","1");
                result.put("message","上传文件为空！");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
    @GetMapping("/customer/customerList")
    public List<Customer> customerList(){
        List<Customer> list = customerService.list();
        System.out.println("/customer/customerList>>>"+list);
        return list;
    }
    @GetMapping("/customer")
    public Map getList(Integer page, Integer limit){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Customer> pages = new Page<Customer>();
        pages.setSize(limit);
        pages.setCurrent(page);
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        String roleName = null;
        for (Role role : employee.getRoleList()) {
            roleName  = role.getRoleName();
        }
        if(roleName.equals("总经理") ){
            IPage<Customer> iPage =  customerService.selectList(pages,null);
            map.put("msg","");
            map.put("count",iPage.getTotal());
            map.put("data",iPage.getRecords());
            map.put("code",0);
            return  map;
        }else {
            String empName = employee.getEmpName();
            IPage<Customer> iPage = customerService.selectList(pages,empName);
            map.put("msg","");
            map.put("count",iPage.getTotal());
            map.put("data",iPage.getRecords());
            map.put("code",0);
            return map;
        }
    }
    @GetMapping("/customer/del")
    public Map selectDelList(Integer page, Integer limit){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Customer> pages = new Page<Customer>();
        pages.setSize(limit);
        pages.setCurrent(page);
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        String roleName = null;
        for (Role role : employee.getRoleList()) {
            roleName  = role.getRoleName();
        }
        if(roleName.equals("总经理") ){
            IPage<Customer> iPage =  customerService.selectDelList(pages,null);
            map.put("msg","");
            map.put("count",iPage.getTotal());
            map.put("data",iPage.getRecords());
            map.put("code",0);
            return  map;
        }else {
            String empName = employee.getEmpName();
            IPage<Customer> iPage = customerService.selectDelList(pages,empName);
            map.put("msg","");
            map.put("count",iPage.getTotal());
            map.put("data",iPage.getRecords());
            map.put("code",0);
            return map;
        }
    }
    @PostMapping("/customer")
    public Map add(Customer customer){
        Map<String,Object> result = new HashMap<String,Object>();
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        customer.setEmpId(employee.getEmpId());
        customer.setCreateTime(StringUtils.getNowTime());
        customer.setUpdateTime(customer.getCreateTime());
        customer.setIsDel(0);
        result.put("state",customerService.save(customer));
        return result;
    }
    @PutMapping("/customer")
    public Map update(Customer customer){
        Map<String,Object> result = new HashMap<String,Object>();
        customer.setUpdateTime(StringUtils.getNowTime());
        result.put("state",customerService.updateById(customer));
        return result;
    }
    @DeleteMapping("/customer/del/{customerId}")
    public Map del(@PathVariable Integer customerId){
        Map<String,Object> result = new HashMap<String,Object>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("customer_id",customerId);
        Customer customer = new Customer();
        customer.setIsDel(1);
        result.put("state",customerService.update(customer,queryWrapper));
        return result;
    }
    @DeleteMapping("/customer/recover/{customerId}")
    public Map recover(@PathVariable Integer customerId){
        Map<String,Object> result = new HashMap<String,Object>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("customer_id",customerId);
        Customer customer = new Customer();
        customer.setIsDel(0);
        result.put("state",customerService.update(customer,queryWrapper));


        return result;
    }
}
